<?php


namespace app\api\controller\v1;

use app\api\model\UserAssetsAuth;
use app\api\model\UserEducationAuth;
use app\api\model\UserIncomeAuth;
use app\api\model\UserJobAuth;
use app\api\model\UserRealAuth;
use app\api\service\Token;
use app\api\validate\auth\CheckAssets;
use app\api\validate\auth\CheckEducation;
use app\api\validate\auth\CheckIncome;
use app\api\validate\auth\CheckJob;
use app\api\validate\auth\CheckReal;

class UserToken
{
    /**
     * 实名认证
     */
    public function createUserReal($just_img_id,$back_img_id)
    {
        (new CheckReal())->goCheck();
        $userInfo = Token::judgeUser();
        $userReal = UserRealAuth::where('openid',$userInfo['openid'])->find();
        if($userReal) {
            $userReal->just_img_id = $just_img_id;
            $userReal->back_img_id = $back_img_id;
            $userReal->status = 2;
            $userReal->sta_msg = '';
            $userReal->save();
            return writeJson(201,[],'提交成功!');
        } else {
            UserRealAuth::create([
                'openid' => $userInfo['openid'],
                'just_img_id' => $just_img_id,
                'back_img_id' => $back_img_id,
                'status' => 2,
            ]);
            return writeJson(201,[],'提交成功!');
        }
    }

    /**
     * 学历认证
     */
    public function createUserEducation($verification_code)
    {
        (new CheckEducation())->goCheck();
        $userInfo = Token::judgeUser();
        $userEducation = UserEducationAuth::where('openid',$userInfo['openid'])
            ->find();
        if($userEducation) {
            $userEducation->verification_code = $verification_code;
            $userEducation->status = 1;
            $userEducation->save();
        } else {
            UserEducationAuth::create([
                'openid' => $userInfo['openid'],
                'verification_code' => $verification_code,
                'status' => 1
            ]);
        }
        return writeJson(201,[],'提交成功!');
    }

    /**
     * 工作认证
     */
    public function createUserJob($company_name, $job_img_id)
    {
        (new CheckJob())->goCheck();
        $userInfo = Token::judgeUser();
        $userJob = UserJobAuth::where('openid',$userInfo['openid'])
            ->find();
        if($userJob) {
            $userJob->company_name = $company_name;
            $userJob->job_img_id = $job_img_id;
            $userJob->status = 1;
            $userJob->save();
        } else {
            UserJobAuth::create([
                'openid' => $userInfo['openid'],
                'company_name' => $company_name,
                'job_img_id' => $job_img_id,
                'status' => 1
            ]);
        }
        return writeJson(201,[],'提交成功!');
    }

    /**
     * 收入证明
     */
    public function createUserIncome($income, $income_img_id)
    {
        (new CheckIncome())->goCheck();
        $userInfo = Token::judgeUser();
        $userIncome = UserIncomeAuth::where('openid',$userInfo['openid'])
            ->find();
        if($userIncome) {
            $userIncome->income = $income;
            $userIncome->income_img_id = $income_img_id;
            $userIncome->status = 1;
            $userIncome->save();
        } else {
            UserIncomeAuth::create([
                'openid' => $userInfo['openid'],
                'income' => $income,
                'income_img_id' => $income_img_id,
                'status' => 1
            ]);
        }
        return writeJson(201,[],'提交成功!');
    }

    /**
     * 资产认证
     */
    public function createUserAssets($assets,$assets_img_id)
    {
        (new CheckAssets())->goCheck();
        $userInfo = Token::judgeUser();
        $userIncome = UserAssetsAuth::where('openid',$userInfo['openid'])
            ->find();
        if($userIncome) {
            $userIncome->assets = $assets;
            $userIncome->assets_img_id = $assets_img_id;
            $userIncome->status = 1;
            $userIncome->save();
        } else {
            UserAssetsAuth::create([
                'openid' => $userInfo['openid'],
                'assets' => $assets,
                'assets_img_id' => $assets_img_id,
                'status' => 1
            ]);
        }
        return writeJson(201,[],'提交成功!');
    }
}